//登录与注册的模块
import { reqGetCode,reqUserRegister,reqUserLogin,reqUserInfo,reqLogout } from '@/api' 
import { setToken, getToken,removeToken} from "@/utils/token";
export default {
	namespaced:true, //开启命名空间
	actions:{
	    //获取验证码
		async getCode(context, phone) {
			 //获取验证码的这个接口：把验证码返回，但是正常情况，后台把验证码发到用户手机上【省钱】
			 let result = await reqGetCode(phone);
		  	 if (result.code == 200) {
				context.commit("GETCODE", result.data);
			    return "ok";
		   } else {
		      return Promise.reject(new Error("faile"));
		   }
		},
		//用户注册
		async userRegister(context, user) {
			console.log("用户注册信息====>",user);
			let result = await reqUserRegister(user);
			if (result.code == 200) {
				return "ok";
			} else {
				return Promise.reject(new Error("faile"));
			}
		},
		 //登录业务
		async userLogin(context , data) {
			let result = await reqUserLogin(data);
			//服务器下发token，用户唯一标识符(uuid)
			//将来经常通过带token找服务器要用户信息进行展示
			if (result.code == 200) {
			  //用户已经登录成功且获取到token
			  context.commit("USERLOGIN", result.data.token);
			  //持久化存储token
			  setToken(result.data.token);
			  return "ok";
			} else {
			  return Promise.reject(new Error("faile"));
			}
		}, 	 
		//获取用户信息
		async getUserInfo(context) {
			let result = await reqUserInfo();
			if (result.code == 200) {
			  //提交用户信息
			  context.commit("GETUSERINFO", result.data);
			  return 'ok';
			}else{
			   return Promise.reject(new Error('faile'));
			}
		},
		 //退出登录
		async userLogout(context) {
			//只是向服务器发起一次请求，通知服务器清除token
			let result = await reqLogout();
			//action里面不能操作state，提交mutation修改state
			if(result.code==200){
			   context.commit("CLEAR");
			  return 'ok';
			}else{
			  return Promise.reject(new Error('faile'));
			}
		  },
	},

	mutations:{
	  GETCODE(state, code) {
		state.code = code;
	  }, 
	  USERLOGIN(state, token) {
		state.token = token;
	  }, 
	  GETUSERINFO(state, userInfo) {
		state.userInfo = userInfo;
	  },
	  //清除本地数据
	  CLEAR(state){
		state.token = '';
		state.userInfo={};
		//本地存储数据清空
		removeToken();
	  }	
	},

	state:{ 
	  code: "",
	  token: getToken(),
	  userInfo:{}
	},

	getters:{
	  
	},

}